package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sapCallback;

import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.service.UnifiedInventoryService;
import com.epoch.app.otsapplicationadaptersdk.mncurrentaccount.service.MnCurrentAccountService;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CurrentAccountCallbackRequest;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CurrentAccountData;
import com.epoch.app.otsapplicationadaptersdk.service.SapService;
import com.epoch.app.otsapplicationreportsdk.cometomonthreport.dto.QueryRegisterProvinceNameByCodeRequest;
import com.epoch.app.otsapplicationreportsdk.cometomonthreport.dto.QueryRegisterProvinceNameByCodeResponse;
import com.epoch.app.otsapplicationreportsdk.cometomonthreport.model.ComeToMonthReport;
import com.epoch.app.otsapplicationreportsdk.cometomonthreport.service.ComeToMonthReportService;
import com.epoch.app.otsapplicationreportsdk.model.dto.ComeToMonthReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthReportConfigLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.monthreportconfig.model.MonthReportConfig;
import com.epoch.app.otsapplicationreportsdk.monthreportconfig.service.MonthReportConfigService;
import com.epoch.app.otsapplicationreportsdk.cometomonthreport.dto.DeleteRequest;
import com.epoch.app.otsplatformenhance.opecustomerindex.service.OpeCustomerIndexService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hhj
 * @description: 平台请求sap往来账创建 ,刷新数据->SAP接口
 * @date 2023/10/7 21:24
 */
@Component
@Primary
public class MnCurrentAccountServiceImpl implements MnCurrentAccountService {
    private final Log log = Log.getLogger(MnCurrentAccountServiceImpl.class);

    @Resource
    private UnifiedInventoryService unifiedInventoryService;

    // 查询 配置部门Service
    @Resource
    private MonthReportConfigService monthReportConfigService;
    // 保存 往来账Service
    @Resource
    private ComeToMonthReportService comeToMonthReportService;
    @Resource
    private SapService sapService;
    @Resource
    private OpeCustomerIndexService customerIndexService;

    @Override
    @ProcessInvoker(remark = "MnCurrentAccountService", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public Result<Void> callback(CurrentAccountCallbackRequest currentAccountCallbackRequest) {

        log.info("MnCurrentAccountService.callback.request:{}", JSON.toJSONString(currentAccountCallbackRequest));

        List<CurrentAccountData> currentAccountDatas = currentAccountCallbackRequest.getData();

        // 查询是否拥有数据
        ComeToMonthReportLoadListRequest comeToMonthReportLoadListRequest = new ComeToMonthReportLoadListRequest();
        comeToMonthReportLoadListRequest.setYearMonth(currentAccountCallbackRequest.getI_MONTH());
        comeToMonthReportLoadListRequest.setKunnr(currentAccountDatas.get(0).getKUNNR());
        Result<List<ComeToMonthReport>> listResult1 = comeToMonthReportService.loadList(comeToMonthReportLoadListRequest);
        List<ComeToMonthReport> result = listResult1.getResult();
        if (result.size()>0){
            // 拥有则需要删除
            result.forEach(e->{
                DeleteRequest deleteRequest = new DeleteRequest();
                deleteRequest.setId(e.getId());
                comeToMonthReportService.delete(deleteRequest);
            });
        }
        // 新增/覆盖的数据
        currentAccountDatas.forEach(e -> {
            ComeToMonthReport comeToMonthReport = new ComeToMonthReport();
            // 由于字段忘转换为小写了，这里手动赋值
            comeToMonthReport.setYearMonth(currentAccountCallbackRequest.getI_MONTH());
            comeToMonthReport.setRbukrs(e.getRBUKRS());
            comeToMonthReport.setPrctr(e.getPRCTR());
            comeToMonthReport.setVkgrp(e.getVKGRP());
            comeToMonthReport.setKunnr(e.getKUNNR());
            comeToMonthReport.setVkbur(e.getVKBUR());
            QueryRegisterProvinceNameByCodeRequest request = new QueryRegisterProvinceNameByCodeRequest();
            request.setCode(e.getKUNNR());
            QueryRegisterProvinceNameByCodeResponse queryRegisterProvinceNameByCodeResponse = comeToMonthReportService.queryRegisterProvinceNameByCode(request);
            if (queryRegisterProvinceNameByCodeResponse.getRegisterProvinceName().size()>0) {
                if (queryRegisterProvinceNameByCodeResponse.getRegisterProvinceName().size()>0){
                    comeToMonthReport.setCustName(queryRegisterProvinceNameByCodeResponse.getRegisterProvinceName().get(0).toString());
                }
            }
            comeToMonthReport.setQcje(e.getQCJE());
            comeToMonthReport.setBqxs(e.getBQXS());
            comeToMonthReport.setBqhk(e.getBQHK());
            comeToMonthReport.setQmye(e.getQMYE());
            comeToMonthReport.setHtlyj(e.getHTLYJ());
            comeToMonthReport.setChbzj(e.getCHBZJ());
            comeToMonthReport.setBgyj(e.getBGYJ());
            log.info("comeToMonthReport:{}",JSON.toJSONString(comeToMonthReport));
            comeToMonthReportService.create(comeToMonthReport);
        });
        return null;
    }

}
